<?php defined('SYSPATH') or die('No direct script access.');

class Model_Click extends ORM
{  
    protected $_reload_on_wakeup = false;

    public function add_click_if_cashback_available(Model_Shop $shop)
    {
        $user = Model_Auth::instance()->get_user();
        if (!$user) {
            return false;     
        }
        if (!$shop->cashback_payment_size) {
            return false;    
        }
        $this->user_id = $user->id;
        $this->shop_id = $shop->id; 
        $this->time    = time();
        $this->save();
        
        return $this;   
    } 
    
    public function get_last_clicks()
    {
        $user = Model_Auth::instance()->get_user();
        if (!$user) {
            throw new Model_Exception('Вы дожны быть авторизованы');     
        } 
        
        $clicks = ORM::factory('click')
                    ->where('user_id', '=', $user->id)
                    ->order_by('time', 'DESC')
                    ->limit(50)
                    ->find_all();
        return $clicks;  
    }
}
